/////////////////////////////////////////////////////////////////////////////
// Purpose:     
// Author:      Jesus Gonzalez
// Modified by: 
// Copyright:   
// License:     
/////////////////////////////////////////////////////////////////////////////

#include <SmartLib/SysLog.h>
#include <SmartLib/File.h>

#include <syslog.h>

SysLog::SysLog(bool consoleLog)
: m_consoleLog(consoleLog)
{
}

static int convert_prio(int sl_prio)
{
    switch(sl_prio)
    {
    case LOG_PRIORITY_ERROR:
        return LOG_ERR;
    case LOG_PRIORITY_WARN:
        return LOG_WARNING;
    case LOG_PRIORITY_INFO:
        return LOG_INFO;
    default:
        return LOG_DEBUG;
    }
}

bool SysLog::Process( int prio, const char* category, const char* function, const char* msg )
{
    int sys_prio = convert_prio(prio);

    if( category == NULL )
    {
        // If the category is the same than the program invocation name, we can skip
        syslog( sys_prio, "%s", msg );
    }
    else
    {
        syslog( sys_prio, "{%s} %s", category, msg );
    }

    return m_consoleLog;
}

void SysLog::Setup(bool consoleLog)
{
    LogHandler::Ptr syslog = SysLog::New(consoleLog);
    Log::setLogHandler( syslog );
}
